package com.cy.pj.sys.web.service.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(1)
@Aspect
@Component
public class SysTimeAspect {
    @Pointcut("@annotation(com.cy.pj.sys.web.common.annotation.RequiredTime)")
    public void doTime(){}
    @Before("doTime()")
    public void doBefore(){
        System.out.println("@Before");
    }
    /**@After描述的方法*/
    @After("doTime()")
    public void daAfter(){
        System.out.println("@After");
    }

    /**
     * @AfterReturning注解描述的方法，在目标方法正常结束时执行
     * 例如 可以再这样的方法中进行缓存更新操作()
     */
    @AfterReturning("doTime()")
    public void doAfterReturning(){
        System.out.println("@AfterReturning");
    }

    @AfterThrowing("doTime()")
    public void doAfterThrowing(){
        System.out.println("@AfterThrowing");
    }

    @Around("doTime()")
    public Object doAround (ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("SysTimeAspect.doAround.Before");
        try {
            System.out.println("@Around.before");
            Object result = joinPoint.proceed();
            System.out.println("@Around.AfterReturning");
            return result;
        } catch (Throwable e) {
            System.out.println("@Around.AfterThrowing");
            e.printStackTrace();
            throw e;
        }finally {
            System.out.println("@Around.After");
        }
    }
}
